package zero.tech.games.pdk.work.queue;

import java.util.ArrayList;
import java.util.List;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.Resp;
import zero.tech.core.event.service.api.EventService;
import zero.tech.core.work.QueueWork;
import zero.tech.core.work.WorkQueue;
import zero.tech.games.common.BigCalSource;
import zero.tech.games.common.Code;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.LogicEventType;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.games.logic.data.consts.LOGICCMD;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.pdk.data.PdkGameBase;
import zero.tech.games.pdk.data.PdkRoomCache;
import zero.tech.games.pdk.data.PdkTimer;
import zero.tech.games.pdk.data.consts.PDKCMD;
import zero.tech.games.pdk.util.PlaybackUtil;
import zero.tech.wsnet.controller.LogicController;
import zero.tech.wsnet.data.Message;

public class AfterGameOverCalculateQueueWork extends QueueWork{
	private Room room;
	private int source;
	@Override
	public void init(Object... objs) throws Exception {
		room=(Room) objs[0];
		source=(int) objs[1];
	}

	@Override
	public void run() {
		PlaybackUtil.saveClubBigPlayback(room,source);//保存大局战绩
		if(source==BigCalSource.DISSOLUTION_BIG_CAL) {
			dissolutionBigCal(room);
		}else if(source==BigCalSource.SMALLOVER_BIG_CAL) {
			smalloverBigCal(room);
		}
	}

	private void smalloverBigCal(Room room) {
		List<Integer> kick_uids=clubMoneyNotEnough(room);
		LogicUtil.broadRoomAllControllersInclude(room, Message.build(PDKCMD.NOTIFY_BIG_CALCULATE, Resp.build(Code.OK, "大结算数据",PckUtil.packageNotifyBigCalculate(room))));		
		//大结算后重新初始化房间代码在这里
		bigResetRoom(room,kick_uids);
	}

	private void dissolutionBigCal(Room room) {
		List<Integer> kick_uids=new ArrayList<>();
		kick_uids.addAll(room.getAllControllers().keySet());
		Message msg=Message.build(PDKCMD.NOTIFY_BIG_CALCULATE, Resp.build(Code.OK, "大结算数据",PckUtil.packageNotifyBigCalculate(room)));
		LogicUtil.broadRoomAllControllersInclude(room, msg);
		LogicUtil.freeRoom(room,Message.build(LOGICCMD.NOTIFY_DISSOLUTION_ROOM_ARRIVED, Resp.build(Code.OK, "解散到达",PckUtil.packageNotifyDissolutionRoomArrived(1))));
	}

	private List<Integer> clubMoneyNotEnough(Room room) {
		List<Integer> kick_uids=new ArrayList<>();
		if(room.getClub()!=null) {
			EventService eventService = ZeroContext.getInstance(EventService.class);
			for(LogicController controller:room.getAllControllers().values()) {
				Boolean ok=eventService.engineCallBack(LogicEventType.CHECK_ONCEMORE_CLUB_ROOM_NEEDMONEY, room,controller);
				if(ok==null||!ok) {
					kick_uids.add(controller.getId());
				}
			}
		}
		return kick_uids;
	}

	private void bigResetRoom(Room room, List<Integer> kick_uids) {
		String room_id=room.getRoom_id();
		PdkRoomCache pdkRoomCache = new PdkRoomCache(room_id);
		PdkGameBase pdkGameBase = new PdkGameBase(room_id);
		PdkTimer pdkTimer = new PdkTimer(room_id);
		room.setRoomCache(pdkRoomCache);
		room.setTimer(pdkTimer);
		room.setGameBase(pdkGameBase);
		room.setRoom_status(RoomStatus.DEFAULT);
		room.setRoom_times(0);
		for(LogicController controller:room.getAllControllers().values()) {
			controller.setWin(0L);//输赢清0
		}
		LogicUtil.broadRoomAllControllersInclude(room, Message.build(PDKCMD.NOTIFY_BIG_RESET, Resp.build(Code.OK, "大结算重置",PckUtil.packageNotifyBigReset(room,kick_uids))));
		for(Integer uid:kick_uids) {
			LogicController logicController=room.getAllControllers().get(uid);
			LogicUtil.freeRoomController(room, logicController, false);
		}
	}

	@Override
	public WorkQueue getWorkQueue() {
		return new WorkQueue() {
			
			@Override
			public long getId() {
				return room.getQueue_id();
			}
		};
	}

}
